package net.jiastudy.biz.user.entity;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotations.TableField;

import io.swagger.annotations.ApiModelProperty;
import net.jiastudy.biz.enums.Gender;
import net.jiastudy.biz.sys.entity.Dept;
import net.jiastudy.biz.sys.util.DictGroup;
import net.jiastudy.biz.sys.util.DictUtils;
import net.jiastudy.core.base.BaseEntity;

public class User extends BaseEntity<User> implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2394648134054461242L;

	@ApiModelProperty(value = "姓名")
	private String name;

	@NotEmpty(message = "用户名不能为空")
	@ApiModelProperty(value = "用户名")
	private String username;

	@ApiModelProperty(value = "昵称")
	private String nickname;

	@ApiModelProperty(value = "email")
	@Email(message = "email格式不正确")
	private String email;

	@ApiModelProperty(value = "密码")
	@JSONField(serialize = false)
	private String password;

	@ApiModelProperty(value = "性别")
	private Gender gender;
	@ApiModelProperty(value = "电话")
	private String phone;
	@ApiModelProperty(value = "手机号")
	private String mobile;
	@ApiModelProperty(value = "备注")
	private String remark;
	@ApiModelProperty(value = "是否禁用")
	private Boolean disable;
	@ApiModelProperty(value = "职称")
	private String title;
	@ApiModelProperty(value = "部门")
	private Long deptId;
	
	@TableField(exist = false)
	private Dept dept;

	@TableField(exist = false)
	private String powerby;
	@TableField(exist = false)
	@ApiModelProperty(value = "党政要点是否为最上一级任务")
	private Boolean toptaskGovernment;

	@TableField(exist = false)
	@ApiModelProperty(value = "是否超级管理员")
	private boolean admin;

	@TableField(exist = false)
	@ApiModelProperty(value = "角色列表")
	private Set<Role> roles;
	@TableField(exist = false)
	@ApiModelProperty(value = "权限列表")
	private Set<String> permissions;

	public Set<String> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<String> permissions) {
		this.permissions = permissions;
	}
	
	public Dept getDept() {
		if (null != deptId) {
			dept = new Dept();
			dept.setId(deptId);
			dept = dept.selectById();
		}
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Boolean getDisable() {
		return disable;
	}

	public void setDisable(Boolean disable) {
		this.disable = disable;
	}

	public String getTitle() {
		return title;
	}

	public String getTitleDesc() {
		return DictUtils.getDictValue(DictGroup.userTitle, title);
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Set<GrantedAuthority> authorities = new HashSet<>();
		if (null == this.getRoles()) {
			return null;
		}
		this.getRoles().forEach(r -> authorities.add(new SimpleGrantedAuthority(r.getCode())));
		return authorities;
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public Gender getGender() {
		return gender;
	}

	public String getGenderDesc() {
		if (null == this.gender) {
			return null;
		}
		return gender.getDesc();
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPowerby() {
		return powerby;
	}

	public void setPowerby(String powerby) {
		this.powerby = powerby;
	}

	public Boolean getToptaskGovernment() {
		return toptaskGovernment;
	}

	public void setToptaskGovernment(Boolean toptaskGovernment) {
		this.toptaskGovernment = toptaskGovernment;
	}
}
